﻿var selectRowIndex = [];
var tabLoadEventPara = {};

$(document).ready(function () {
    $("[data-stopPropagation]").on("click", function (e) {
        e.stopPropagation();
    });
    
    // 注册切换按钮事件
    $(':checkbox.switch-box').bootstrapSwitch();
    $(':checkbox.switch-box[switch-event]').on("switchChange.bootstrapSwitch",
        function (event, state) {
            var eventName = $(this).attr("switch-event");
            eval(eventName)(event, state);
        });

    //日期选择控件置为只读
    $("input[data-role='datepicker']").attr("readonly", "readonly");
    $("div[data-picker='datepicker']").find("input").attr("readonly", "readonly");

    var initDatePicker = function (e) {
        var $this = $(e);
        var startDate = ($this.data("startdate") == undefined ? $this.find("input").data("startdate") : undefined)
            ? $this.find("input").data("startdate")
            : "1999-01-01";
        var endDate = ($this.data("enddate") == undefined ? $this.find("input").data("enddate") : undefined)
            ? $this.find("input").data("enddate")
            : "9999-12-31";
        var selectEvent = $this.find("input").data("onchanged");

        $.fn.datepicker.call($this,
            {
                autoclose: true,
                language: "zh-CN",
                startDate: startDate,
                endDate: endDate,
                clearBtn: true,

            });
        if (selectEvent !== "" && selectEvent != undefined) {
            $this.datepicker().on('changeDate', function (e) {
                eval(selectEvent)(e,$this);
            });
        }

        var value = $this.find("input").val();

        if (value !== "") {
            var oldDate = $this.find("input").val().ToDateTime();
            oldDate.setSeconds(oldDate.getSeconds() + 1);

            $this.datepicker('setDate', oldDate);
        }
    }

    //初始化
    $(document).on(
        'focus.datepicker.data-api click.datepicker.data-api',
        '[data-picker="datepicker"]',
        function (e) {
            var $this = $(this);
            if ($this.data('datepicker'))
                return;
            e.preventDefault();
            // component click requires us to explicitly show it

            initDatePicker(this);
        }
    );

    $("div[data-picker='datepicker']").each(function () {
        initDatePicker(this);
    });


    //处理提示
    $('[data-toggle="tooltip"]').tooltip();

    //处理InputMask
    $("input").inputmask();


    //初始化下拉列表
    $(".common-select2").select2({
        //minimumResultsForSearch: -1,
        language: 'zh-CN',
        placeholder: '请选择',
        width: '100%',
        formatNoMatches: '没有搜索到想要的结果'
    });


    $("[action]").each(function() {
        var dom = $(this);

        var code = dom.data("action-code");

        if (code !== "" && code !== undefined && code != null) {
            abp.services.admin.navigation.judgHasRoleByNavigationCode(code).done(function(result) {
                if (!result) {
                    dom.hide();
                }
            });
        }
    });
});


function onAuthValidation(code, clickFuncScript, clickEvents, message) {
    if (message == null || message === "") {
        message = "不能进行此操作";
    }
    abp.ajax({
        url: "/api/services/admin/authority/AuthorityValidation?code=" + code + "&text=" + message,
        async: false
    }).done(function (data) {
        if (data.result) {
            if (clickFuncScript) {
                if (typeof clickFuncScript === "string") {
                    eval(clickFuncScript);
                } else {
                    clickFuncScript();
                }
            }

            if (clickEvents != undefined) {
                for (var i = 0; i < clickEvents.length; i++) {
                    clickEvents[i].handler();
                }
            }

            return true;
        } else {
            abp.message.error(data.message, "权限错误提示");
            return true;
        }
    }).fail(function () {
        return false;
    });
}


//窗体重置大小
function onWindowResize() {
    $(window).resize();
}

//当下拉列表只有一个选项时，默认选择第一个
function onDropDownListBound(e) {
    if (e.sender.dataItems().length === 1) {
        e.sender.value(e.sender.dataItems()[0].Id);
        e.sender.optionLabel.remove();
    }
}

//记录已选择的grid行
function createSelectRowIndex(grid) {
    if (grid) {
        selectRowIndex = [];
        var name = grid.element.attr("id");

        if (grid.dataItems().length > 0) {
            if (grid.options.selectable) {
                $(grid.select()).each(function (i, e) {
                    selectRowIndex.push({
                        name: name,
                        value: $(e).index()
                    });
                });
            }
        }
    }
}

//选择记录的选择行
function execSelectRow(grid) {
    var name = grid.element.attr("id");

    var indexList = selectRowIndex.filter(function (e) {
        return e.name === name;
    });

    $(indexList).each(function (i, e) {
        var row = grid.tbody.find(">tr:not(.k-grouping-row)").eq(e.value);
        grid.select(row);
    });
}


//重绘窗体
function gridResize() {
    $(".k-grid").each(function () {
        //var contentHeight = $(this).height() -
        //    ($(this).find(".k-grid-toolbar") == null ? 0 : $(this).find(".k-grid-toolbar").outerHeight()) -
        //    ($(this).find(".k-grid-header") == null ? 0 : $(this).find(".k-grid-header").outerHeight()) -
        //    ($(this).find(".k-pager-wrap") == null ? 0 : $(this).find(".k-pager-wrap").outerHeight());

        //$(this).find(".k-grid-content-locked").height(contentHeight);
        //$(this).find(".k-grid-content").height(contentHeight);

        onGridResize(this);
    });
}

function onGridResize(gridTable) {
    var grid = $(gridTable).data("kendoGrid") ? $(gridTable).data("kendoGrid") : $(gridTable).data("kendoTreeList");
    if (grid) {
        //  createSelectRowIndex(grid);
        // grid.refresh();
        grid.resize();
    }
}

function splitterResize() {
    $(".k-splitter").each(function () {
        var splitter = $(this).data("kendoSplitter");
        splitter.resize();
    });
}


function onCommonProcessData(json) { // url 获取数据时，对数据的处理，作为 getData 的回调函数
    var i, data = { value: [] };
    if (!json || json.length === 0) {
        return false;
    }
    var len = json.length;
    for (i = 0; i < len; i++) {
        data.value.push(json[i]);
    }
    return data;
}